/*
 * Copyright (C) 2013 TopCoder Inc., All Rights Reserved.
 */
package cn.itcast.heima;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author TCSASSEMBLER
 * @version 1.0 ()
 * @since 1.0
 */
public class ThreadLocalTest {
	private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
	private static ThreadLocal<MyThreadScopeData> threadLocal2 = new ThreadLocal<MyThreadScopeData>();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i = 0; i < 2; i++)
		new Thread(
				new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int data = new Random().nextInt();
						System.out.println(Thread.currentThread().getName()
								+ "has put data: " + data);
						threadLocal.set(data);
						MyThreadScopeData myData = new MyThreadScopeData();
						myData.setName("name" + data);
						myData.setAge(data);
						new A().get();
						new B().get();
					}
				}
		).start();
	}
	
	static class A {
		public void get() {
			int data = threadLocal.get();
			System.out.println("A from " + Thread.currentThread().getName()
					+ "get data: " + data);
			MyThreadScopeData myData = new MyThreadScopeData();
			myData.setName("name" + data);
			myData.setAge(data);
		}
	}
	
	static class B {
		public void get() {
			int data = threadLocal.get();
			System.out.println("B from " + Thread.currentThread().getName()
					+ "get data: " + data);
			
		}
	}

}

class MyThreadScopeData {
	private String name;
	private int age;
	/**
	 * <p>
	 * Getter of name field.
	 * </p>
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * <p>
	 * Setter of name field.
	 * </p>
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * <p>
	 * Getter of age field.
	 * </p>
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * <p>
	 * Setter of age field.
	 * </p>
	 * @param data the age to set
	 */
	public void setAge(int data) {
		this.age = data;
	}
}


